import toml
import pathlib
from dataclasses import dataclass

# 获取读取目录的父目录
path = pathlib.Path(__file__).parent


# 读取配置文件
def load_config(file_path=path.parent.joinpath("./config/config.toml"), c=None):

    with open(file_path, "r", encoding="utf-8") as f:
        return toml.load(f) if c is None else toml.load(f, decoder=c)


# mysql配置类
@dataclass
class MysqlConfig:
    @dataclass
    class Database:
        default: str

    host: str
    port: int
    user: str
    password: str
    database: Database


@dataclass
class RedisConfig:
    host: str
    port: str
    password: str


# 总配置类
@dataclass
class Config:
    mysql: MysqlConfig
    redis: RedisConfig

    def __init__(self):
        # mysql配置
        self.mysql = MysqlConfig(**config_dict["mysql"])
        self.mysql.database = MysqlConfig.Database(**config_dict["mysql"]["database"])

        # redis配置
        self.redis = RedisConfig(**config_dict["redis"])


config_dict = load_config()
config = Config()

if __name__ == "__main__":
    print(config)
    print(config.mysql.database.default)
